package www.nupter.org.njuptask.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

import www.nupter.org.njuptask.R;
import www.nupter.org.njuptask.adapter.CommonAdapter;
import www.nupter.org.njuptask.http.HttpCallBack;
import www.nupter.org.njuptask.http.HttpRequestUtil;
import www.nupter.org.njuptask.http.RequestUrl;
import www.nupter.org.njuptask.model.Questions;
import www.nupter.org.njuptask.model.ReplyInfo;
import www.nupter.org.njuptask.utils.ViewHolder;
import www.nupter.org.njuptask.view.NoScrollListview;


/**
 * A login screen that offers login via email/password.
 */
public class QuestionDetailActivity extends BaseActivity {

    private TextView titleTextView;
    private TextView authorTextView;
    private TextView contentTextView;
    private ImageView imageView;
    private int questionId;

    private NoScrollListview noScrollListview;
    private CommonAdapter<ReplyInfo.DataEntity> commonAdapter;
    private List<ReplyInfo.DataEntity> replyList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_detail);
        setTitle("详细信息");
        setRightText("回复");
        titleTextView=(TextView)findViewById(R.id.title);
        authorTextView=(TextView)findViewById(R.id.author);
        contentTextView=(TextView)findViewById(R.id.content);
        imageView=(ImageView)findViewById(R.id.image);
        noScrollListview=(NoScrollListview)findViewById(R.id.noscrowview);
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bun");
        int id=bundle.getInt("id");
        String name=bundle.getString("name");
        String content=bundle.getString("content");
        final String file=bundle.getString("file");
        String title=bundle.getString("title");
        questionId=bundle.getInt("questionId");
        titleTextView.setText(title);
        authorTextView.setText(name);
        contentTextView.setText(content);
        if (file.equals("1")){
            imageView.setVisibility(View.GONE);
        }else {
            HttpRequestUtil.initImage(imageView,file);
        }
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("image", file);
                intent.putExtra("i", "0");
                intent.putExtra("url_array", file);
                intent.setClass(QuestionDetailActivity.this, ImageDetail.class);
                startActivity(intent);
            }
        });

        commonAdapter=new CommonAdapter<ReplyInfo.DataEntity>(QuestionDetailActivity.this,replyList,R.layout.reply_item) {
            @Override
            public void convert(ViewHolder holder, ReplyInfo.DataEntity item, int position) {
                TextView authorText = holder.getView(R.id.author_name);
                TextView content = holder.getView(R.id.des);
                authorText.setText(item.getName());
                content.setText(item.getContent());
            }
        };
        noScrollListview.setAdapter(commonAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getReply(String.valueOf(questionId));
    }

    public void getReply(String id) {
        final RequestParams requestParams = new RequestParams(RequestUrl.list_reply);
        requestParams.addBodyParameter("questionId",id);
        HttpRequestUtil.getMethod(requestParams, ReplyInfo.class, new HttpCallBack<ReplyInfo>() {
            @Override
            public void success(ReplyInfo data) {
                replyList.clear();
                replyList.addAll(data.getData());
                commonAdapter.notifyDataSetChanged();
            }

            @Override
            public void erro(String res) {

            }
        });
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        if (component == TitleBar.LEFT) {
            finish();
        }if (component==TitleBar.RightText){
            Intent intent=new Intent();
            intent.putExtra("questionId", String.valueOf(questionId));
            intent.setClass(QuestionDetailActivity.this,PublishQuestionReplyActivity.class);
            startActivity(intent);
        }
    }
}

